package base.web.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import base.service.bo.base.model.IBoBaseModel;
import base.service.domain.exception.DaoException;
import base.service.domain.exception.NoSuchBeanException;
import base.service.domain.exception.ServiceLocatorException;

/**
 * @Title: BaseMenu.java
 * @Package base.web.tools
 * @Description: TODO
 * @author YeQing
 * @date 2012-6-10 下午03:20:36
 * @version V1.0
 */
public class BaseMenu {

	public static String getMenuJson() {
		String json = "";
		try {
			Map paramMap = new HashMap();
            paramMap.put("MENU", -1);
			IBoBaseModel boBaseModel = (IBoBaseModel) BeansHelp
					.getBeanInstance("boBaseModel");
			List list = boBaseModel.getListBaseModel(paramMap);
			json = PluSoft.Utils.JSON.Encode(list);
		} catch (NoSuchBeanException e) {
			throw new ServiceLocatorException(e.getMessage(), e);
		} catch (DaoException e) {
			throw new ServiceLocatorException(e.getMessage(), e);
		}
		return json;
	}

	@SuppressWarnings("unchecked")
	public static String getTreeJson(Map paramMap) {
		String json = "";
		try {

            //首次加载
           /* String pid = (String) paramMap.get("PID");
            if(pid.endsWith("0")) {
                paramMap.remove("PID");
                paramMap.put("lazy", "-1");*//*  //加载二级菜单
            }*/
			IBoBaseModel boBaseModel = (IBoBaseModel) BeansHelp
					.getBeanInstance("boBaseModel");
			List list = boBaseModel.getListBaseModel(paramMap);

            //首次加载处理方式
            for (int i = 0, l = list.size(); i < l; i++) {
				HashMap node = (HashMap) list.get(i);
				String nodeId = node.get("id").toString();
				HashMap pMap = new HashMap();
				pMap.put("PID", nodeId);
				List nodes = boBaseModel.getListBaseModel(pMap);

				if (nodes.size() > 0) {
					node.put("isLeaf", false);
					node.put("expanded", false);
				}
                if(((Integer)node.get("pid")).intValue() == 0) {
                    HashMap childMap = new HashMap();
                    childMap.put("PID", "-1");
                    List childNodes = boBaseModel.getListBaseModel(childMap);
                    for(int j=0; j<childNodes.size(); j++) {
                        HashMap childNode = (HashMap) childNodes.get(j);
                        String cChildID = childNode.get("id").toString();
                        HashMap cCMap = new HashMap();
                        cCMap.put("PID", cChildID);
                        List cChilds = boBaseModel.getListBaseModel(cCMap);
                        if(cChilds.size() > 0) {
                            childNode.put("isLeaf", false);
					        childNode.put("expanded", false);
                        }
                    }
                    node.put("children", childNodes);
                    node.put("expanded", true);
                }
			}


            //展开树时处理方式



			json = PluSoft.Utils.JSON.Encode(list);
            System.out.println(json);
		} catch (NoSuchBeanException e) {
			throw new ServiceLocatorException(e.getMessage(), e);
		} catch (DaoException e) {
			throw new ServiceLocatorException(e.getMessage(), e);
		}
		return json;
    }

    public static String getTreeJson2222(Map paramMap) {
		String json = "";
		try {
			IBoBaseModel boBaseModel = (IBoBaseModel) BeansHelp
					.getBeanInstance("boBaseModel");
			List list = boBaseModel.getListBaseModel(paramMap);
			for (int i = 0, l = list.size(); i < l; i++) {
				HashMap node = (HashMap) list.get(i);
				String nodeId = node.get("id").toString();
				HashMap pMap = new HashMap();
				pMap.put("PID", nodeId);
				List nodes = boBaseModel.getListBaseModel(pMap);

				if (nodes.size() > 0) {
					node.put("isLeaf", false);
					node.put("expanded", false);
				}
			}

			// 返回处理结果
			// String json = PluSoft.Utils.JSON.Encode(folders);
			json = PluSoft.Utils.JSON.Encode(list);
		} catch (NoSuchBeanException e) {
			throw new ServiceLocatorException(e.getMessage(), e);
		} catch (DaoException e) {
			throw new ServiceLocatorException(e.getMessage(), e);
		}
		return json;
    }

    public static void  main(String args[]) {
        /*List list0 = new ArrayList();

        Map pMap0 = new HashMap();

        pMap0.put("id", "lists");
        pMap0.put("text", "Lists");

            List childList = new ArrayList();
            Map childParam0 = new HashMap();
            childParam0.put("id", "dataGrid");
            childParam0.put("text", "DataGrid");
            Map childParam1 = new HashMap();
            childParam1.put("id", "tree");
            childParam1.put("text", "Tree");
        childList.add(childParam0);
        childList.add(childParam1);

        pMap0.put("children", childList);

        list0.add(pMap0);*/


        //System.out.println(PluSoft.Utils.JSON.Encode(list0));
        HashMap map = new HashMap();
        map.put("PID", "0");
        BaseMenu.getTreeJson(map);


    }

}
